package com.fasttreatment.zzr.demo.net;

import com.fasttreatment.zzr.demo.model.GankEntity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.util.HashMap;


public class NetApi {

    public static void getGankData(String type, int pageSize, int pageNum, JsonCallback<GankEntity> gankCallback) {
        String gankUrl = Urlkit.getUrl(Urlkit.ACTION_DATA_RESULT);
        gankUrl = gankUrl.replace("{type}", type)
                .replace("{number}", "" + pageSize)
                .replace("{page}", "" + pageNum);
        invokeGet(gankUrl, null, gankCallback);
    }

    private static void invokeGet(String url, HashMap params, Callback callback) {
        OkHttpUtils.get().url(url)
                .params(params == null ? new HashMap<String, String>() : params)
                .build()
                .execute(callback);
    }

    /**
     * @param url      拼接的url
     * @param map      Params的map
     * @param callback 回调
     */
    public static void postGankData(String url, HashMap map, Callback callback) {
        OkHttpUtils
                .post()
                .url(url)
                .params(map)
                .build()
                .execute(callback);
    }

    /**
     * @param url      拼接的url
     * @param file     文件目录
     * @param callback 回调
     */
    public static void postGankfile(String url, File file, Callback callback) {
        OkHttpUtils
                .postFile()
                .url(url)
                .file(file)
                .build()
                .execute(callback);
    }
}
